package svgedit.gui;

import java.awt.Rectangle;
import java.awt.geom.GeneralPath;
import svgedit.svg.SVGElement;

/**
 * This class represents the GUI markers used to move and resize shapes.
 */
public class Marker
{
	/**
	 * enum is used to represent the markers type and determine its position
	 * MOVE is common to all shapes and appears in the centre of all of them
	 * including groups. All the other markers are used to change the size of
	 * the object GROW appears on the right of a circle. It would be used to any
	 * other shape that has just one grow marker. LINE_END1 and LINE_END2
	 * represent the two ends of a line. The RECT markers are self explanatory.
	 * The actual positions of the markers are determined in the class the
	 * marker is representing
	 * 
	 * @see svgedit.svg.SVGDrawable#getSelectionStroke()
	 * @see svgedit.svg.SVGDrawable#drag(double, double, Marker.MarkerType)
	 */
	public enum MarkerType
	{
		MOVE, GROW,

		RECT_TOP_LEFT, RECT_TOP_CENTER, RECT_TOP_RIGHT, RECT_LEFT_CENTRE, RECT_RIGHT_CENTRE, RECT_BOTTOM_LEFT, RECT_BOTTOM_CENTER, RECT_BOTTOM_RIGHT,

		LINE_END1, LINE_END2
	}

	/**
	 * (static) size of all the Markers
	 */
	private static final double MARKER_SIZE = 7f;

	private MarkerType markerType;
	private SVGElement element;
	private Rectangle bounds;

	/**
	 * Creates a new marker for the supplied element. The path supplied is the
	 * selection stroke on which the marker appears. In other words the shape
	 * itself and its markers and selection box are drawn separately by the View
	 * (the shape itself and its selection stroke when required
	 * 
	 * @see svgedit.gui.View#paintElement(java.awt.Graphics2D, SVGElement)
	 * @see svgedit.gui.Marker.MarkerType
	 * @param element
	 *            the element who's markers we are creating
	 * @param path
	 *            the selection stroke to draw onto
	 * @param x
	 *            coordinate to draw the marker
	 * @param y
	 *            coordinate to draw the marker
	 * @param mType
	 *            marker type
	 */
	public Marker(SVGElement element, GeneralPath path, float x, float y,
			MarkerType mType)
	{
		this.element = element;
		this.markerType = mType;
		// we cant to draw the marker around the centre of x and y
		double radius = MARKER_SIZE / 2;

		// create the marker on the stroke
		path.moveTo(x - radius, y - radius);
		path.lineTo(x + radius, y - radius);
		path.lineTo(x + radius, y + radius);
		path.lineTo(x - radius, y + radius);
		path.closePath();

		// Add a new marker listen with the element + marker x,y + marker type
		this.bounds = new Rectangle.Double(x - radius, y - radius, MARKER_SIZE,
				MARKER_SIZE).getBounds();
	}

	/**
	 * Drags the element the distance specified by the offset deltaX,deltaY This
	 * is called by the View and in turn calls the drag method of the element
	 * this marker is attached to.
	 * 
	 * @see svgedit.gui.View#paintElement(java.awt.Graphics2D, SVGElement)
	 * @see svgedit.svg.SVGDrawable#drag(double, double, Marker.MarkerType)
	 * @param deltaX
	 * @param deltaY
	 */
	public void drag(double deltaX, double deltaY)
	{
		this.bounds = this.element.getBounds();
		this.element.drag(deltaX, deltaY, this.markerType);
		Frame.getFrame().modifiedTitle(true);
	}

	/**
	 * @return the bounds of the markers. ie a rectangle representing its
	 *         position
	 */
	public Rectangle getBounds()
	{
		return bounds;
	}

}
